package cn.buildoncloud.labor.workermanapplet.business.modules.services.impl;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.AccessRecordService;
import cn.buildoncloud.labor.workermanapplet.common.base.service.BaseServiceImpl;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import cn.buildoncloud.labor.workermanapplet.dto.AccessRecordDTO;
import cn.buildoncloud.labor.workermanapplet.util.StringUtil;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: cn.buildoncloud.labor.workermanapplet.business.modules.services.impl
 * Created by CuiZibo on 2018/11/16 15:30
 */
@Service
public class AccessRecordServiceImpl extends BaseServiceImpl<Map> implements AccessRecordService {

    public AccessRecordServiceImpl(Dao dao) {
        super(dao);
    }

    /**
     * 进出记录列表
     * @param name
     * @param passed
     * @param personType
     * @param projectId
     * @param date
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Pagination queryAccessRecordList(String name, Integer passed, Integer personType, String projectId, String date, int pageNo, int pageSize) {

        boolean b = this.tableIsExist("bi_access_record_", StringUtil.last2num(projectId));
        if (!b) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT CONCAT(t1.id,'') id ,CASE WHEN(t1.special_person_id IS NOT NULL) THEN CONCAT(t1.`name`, '(工程人员)') WHEN(t1.workerman_id IS NOT NULL) THEN CONCAT(t1.`name`, '(工友)') ELSE t1.no_pass_msg END `name`, ")
                .append("CASE WHEN (t1.special_person_id IS NOT NULL) THEN 2 WHEN (t1.workerman_id IS NOT NULL) THEN 1 ELSE 0 END person_type, ")
                .append("t1.type, CONCAT(t1.record_dtime,'') dt, t1.is_pass , t1.live_photo_path_byBase64 base64, t1.is_offline,CONCAT(t1.workerman_id, '') workerman_id,CONCAT(t1.special_person_id, '') special_person_id,CONCAT(t1.equipment_id, '') equipment_id,CONCAT(t1.project_id, '') project_id ")
                .append("FROM `bi_access_record_$pid` t1 WHERE t1.delFlag = FALSE AND t1.project_id = @projectId ");
        if (StringUtil.isNotBlank(name)) {
            sb.append("AND t1.`name` LIKE @name ");
        }
        if (passed != null) {
            sb.append("AND t1.is_pass = @passed ");
        }
        if (personType != null) {
            if (1 == personType) {
                sb.append("AND t1.workerman_id IS NOT NULL ");
            } else if (2 == personType) {
                sb.append("AND t1.special_person_id IS NOT NULL ");
            }
        }
        if (StringUtil.isNotBlank(date)) {
            sb.append("AND t1.record_dtime BETWEEN @begin AND @end ");
        }
        sb.append("ORDER BY dt DESC ");
        Sql sql = Sqls.create(sb.toString());
        sql.setVar("pid", StringUtil.last2num(projectId))
                .setParam("projectId", projectId)
                .setCallback((conn, rs, sql1) -> {
                    List<AccessRecordDTO> list = new ArrayList<>();
                    while (rs.next()) {
                        AccessRecordDTO accessRecordDTO = new AccessRecordDTO();
                        accessRecordDTO.setId(rs.getString("id"));
                        accessRecordDTO.setName(rs.getString("name"));
                        accessRecordDTO.setIsPass(rs.getInt("is_pass"));
                        accessRecordDTO.setType(rs.getString("type"));
                        accessRecordDTO.setRecordTime(rs.getString("dt"));
                        accessRecordDTO.setPhotoBase64(rs.getString("base64"));
                        accessRecordDTO.setIsOffline(rs.getInt("is_offline"));
                        accessRecordDTO.setWorkerManId(rs.getString("workerman_id"));
                        accessRecordDTO.setSpecialPersonId(rs.getString("special_person_id"));
                        accessRecordDTO.setEquipmentId(rs.getString("equipment_id"));
                        accessRecordDTO.setProjectId(rs.getString("project_id"));
                        accessRecordDTO.setPersonType(rs.getInt("person_type"));
                        list.add(accessRecordDTO);
                    }

                    return list;
                });
        if (StringUtil.isNotBlank(name)) {
            sql.setParam("name", "%" + name + "%");
        }
        if (passed != null) {
            sql.setParam("passed", passed);
        }
        if (StringUtil.isNotBlank(date)) {
            String begin = date;
            String end = date;
            begin = date + " 00:00:00";
            end = date + " 23:59:59";

            sql.setParam("begin", begin)
                    .setParam("end", end);
        }
        Pagination pagination = this.listPage(pageNo, pageSize, sql);
        return pagination;
    }

    /**
     * 进出详情
     * @param id
     * @param projectId
     * @param personType
     * @return
     */
    @Override
    public Map queryAccessRecordDetail(String id, String projectId, int personType) {
        boolean b = this.tableIsExist("bi_access_record_", StringUtil.last2num(projectId));
        if (!b) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        if (personType == 1) {
            sb.append("SELECT CONCAT(t3.`name`,'(',IFNULL(t4.`name`,'无劳务公司'),')') value1,t2.sex,t2.nation,t2.tel,t2.id_card_no,t5.note,t5.business_type,t6.unique_identity ")
                    .append("FROM `bi_access_record_$pid` t1 LEFT JOIN bi_workerman t2 ON t2.id = t1.workerman_id LEFT JOIN bi_workerteam t3 ON t3.id = t2.worker_team_id LEFT JOIN bi_labor_service_company t4 ON t4.id = t3.labor_service_company_id LEFT JOIN re_equipment_and_project t5 ON t5.equipment_id = t1.equipment_id AND t5.project_id = t1.project_id LEFT JOIN sys_equipment t6 ON t6.id = t1.equipment_id ")
                    .append("WHERE t1.project_id = @projectId AND t1.id = @id");
        } else if (personType == 2) {
            sb.append("SELECT t3.`name` value1,t2.sex,t2.nation,t2.tel,t2.id_card_no,t5.note,t5.business_type,t6.unique_identity ")
                    .append("FROM `bi_access_record_$pid` t1 LEFT JOIN bi_special_person t2 ON t2.id = t1.special_person_id LEFT JOIN sys_project_job t3 ON t3.id = t2.job_id LEFT JOIN re_equipment_and_project t5 ON t5.equipment_id = t1.equipment_id AND t5.project_id = t1.project_id LEFT JOIN sys_equipment t6 ON t6.id = t1.equipment_id ")
                    .append("WHERE t1.project_id = @projectId AND t1.id = @id");
        }
        Sql sql = Sqls.create(sb.toString());
        sql.setVar("pid", StringUtil.last2num(projectId))
                .setParam("id", id)
                .setParam("projectId", projectId)
                .setCallback((conn, rs, sql1) -> {
                    List<Map> list = new ArrayList<>();
                    while (rs.next()) {
                        Map map = new HashMap();
                        map.put("value1", rs.getString("value1"));
                        map.put("sex", rs.getInt("sex"));
                        map.put("nation", rs.getString("nation"));
                        map.put("tel", rs.getString("tel"));
                        map.put("id_card_no", rs.getString("id_card_no"));
                        map.put("business_type", rs.getString("business_type"));
                        map.put("equipment_note", rs.getString("note"));
                        map.put("unique_identity", rs.getString("unique_identity"));
                        list.add(map);
                    }
                    return list;
                });
        dao().execute(sql);
        List<Map> list = sql.getList(Map.class);
        list = list == null ? new ArrayList<>() : list;
        if (list.size() == 0)
            return null;
        return list.get(0);
    }
}
